package foo.proj.bo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import foo.dao.ProjDAO;
import foo.dao.SysTypeDAO;
import foo.dbvo.ProjDbvo;
import foo.dbvo.SysTypeDbvo;
import foo.dbvo.WbsDbvo;
import foo.proj.vo.ProjVvo;
import foo.proj.vo.WbsVvo;
import foo.websystem.action.Consts;

@Service
public class ProjBO {
	@Autowired
	ProjDAO projDAO;
	@Autowired
	SysTypeDAO sysTypeDAO;
	LinkedHashMap<String, String> wbsTypeDropDownList;

	public Map<String, String> findWbsTypeDropDown() {
		if (wbsTypeDropDownList == null) {
			List<SysTypeDbvo> list = sysTypeDAO
					.findByTypeGroup(Consts.SysTypeGroup.WBST_YPE);
			wbsTypeDropDownList = new LinkedHashMap<String, String>();
			for (SysTypeDbvo sysTypeDbvo : list) {
				wbsTypeDropDownList.put(sysTypeDbvo.getTypeId(),
						sysTypeDbvo.getDscr());
			}
		}
		return wbsTypeDropDownList;
	}

	public Map<String, String> findProjDropDown(String userId) {
		List<Map<String, Object>> datas = projDAO.findProjDropDownList(userId);
		Map<String, String> dropDown = new LinkedHashMap<String, String>();
		for (Map<String, Object> data : datas) {
			dropDown.put(data.get("sn") == null ? "NULL" : data.get("sn")
					.toString(),
					data.get("name") == null ? "NULL" : data.get("name")
							.toString());
		}
		return dropDown;
	}

	public List<ProjVvo> findProjs(String criteria) {
		List<ProjDbvo> dbvos = projDAO.findProjList();
		ArrayList<ProjVvo> list = new ArrayList<ProjVvo>();
		for (ProjDbvo dbvo : dbvos) {
			ProjVvo vo = new ProjVvo();
			vo.setSn(String.valueOf(dbvo.getSn()));
			vo.setName(dbvo.getName());
			list.add(vo);
		}
		return list;
	}

	public ProjVvo findProjsByPk(String sn) {
		if (StringUtils.isBlank(sn)) {
			throw new NullPointerException("查詢PK為空？");
		}
		ProjDbvo dbvo = projDAO.findProjByPk(Integer.valueOf(sn));
		ProjVvo vo = new ProjVvo();
		vo.setSn(String.valueOf(dbvo.getSn()));
		vo.setName(dbvo.getName());
		vo.setContent(dbvo.getContent());
		return vo;
	}

	public WbsVvo findWbsByPk(String wbsSn) {
		WbsDbvo dbvo = projDAO.findWbsByPk(Integer.valueOf(wbsSn));
		WbsVvo vvo = new WbsVvo();
		vvo.setEstHr(String.valueOf(dbvo.getEstHr()));
		vvo.setName(dbvo.getName());
		vvo.setSn(String.valueOf(dbvo.getSn()));
		vvo.setTypeDscr(dbvo.getTypeDscr());
		vvo.setTypeId(dbvo.getTypeId());
		return vvo;
	}

	public List<WbsVvo> findWbs(String name, String typeId, String projSn) {
		if (StringUtils.isBlank(typeId)) {
			typeId = null;
		}
		if (StringUtils.isBlank(name)) {
			name = null;
		}
		List<WbsDbvo> dbvos = projDAO.findWbsByQuery(name, typeId,
				Integer.valueOf(projSn));
		ArrayList<WbsVvo> vvos = convertWbsDbvo2Vvo(dbvos);
		return vvos;
	}

	public List<WbsVvo> findWbsByProj(String projSn) {
		List<WbsDbvo> dbvos = projDAO.findWbsByQuery(null, null,
				Integer.valueOf(projSn));
		ArrayList<WbsVvo> list = convertWbsDbvo2Vvo(dbvos);
		return list;
	}

	private ArrayList<WbsVvo> convertWbsDbvo2Vvo(List<WbsDbvo> dbvos) {
		ArrayList<WbsVvo> list = new ArrayList<WbsVvo>();
		for (WbsDbvo dbvo : dbvos) {
			WbsVvo vo = new WbsVvo();
			vo.setSn(String.valueOf(dbvo.getSn()));
			vo.setName(dbvo.getName());
			vo.setTypeDscr(dbvo.getTypeDscr());
			vo.setTypeId(dbvo.getTypeId());
			vo.setEstHr(String.valueOf(dbvo.getEstHr()));
			list.add(vo);
		}
		return list;
	}

	/**
	 * 新增或編輯Proj
	 * 
	 * @param sn
	 *            Null表新增
	 * @return projSn
	 */
	public String saveOrUpdateProj(String sn, String name, String content)
			throws Exception {
		if (StringUtils.isBlank(sn)) {
			Integer generatedSn = projDAO.insertProj(name, content);// 產生的SN
			return String.valueOf(generatedSn);
		} else {
			projDAO.updateProj(sn, name, content);
			return sn;
		}
	}

	public void insertWbs(String projSn, String name, String estHr,
			String typeId) {
		Integer projSnInt;
		if (StringUtils.isBlank(projSn)) {
			throw new NullPointerException("無專案");
		}
		Integer estHrInt;
		if (StringUtils.isBlank(estHr)) {
			estHrInt = Integer.valueOf(0);
		} else {
			estHrInt = Integer.valueOf(estHr);
		}
		projSnInt = Integer.valueOf(projSn);
		projDAO.insertWbs(projSnInt, name, typeId, estHrInt);
	}

	public void udpateWbs(String sn, String name, String typeId, String estHr) {
		projDAO.updateWbs(Integer.valueOf(sn), name, typeId,
				Integer.valueOf(estHr));
	}

	public void deleteWbs(String sn) {
		projDAO.deleteWbs(Integer.valueOf(sn));
	}

}
